package com.ssyc.gsk_parents.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.netease.nim.uikit.api.NimUIKit;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.gsk_parents.R;
import com.ssyc.gsk_parents.bean.FriendsInfo;

import java.util.List;


/**
 * 学生端---通讯录--好友列表
 * Created by Administrator on 2018/5/15 0015.
 */

public class FriendsRvAdapter extends BaseQuickAdapter<FriendsInfo, BaseViewHolder> {

    private List<FriendsInfo> data;

    private Context context;


    public FriendsRvAdapter(Context context, int layoutResId, @Nullable List<FriendsInfo> data) {
        super(layoutResId, data);
        this.context = context;
        this.data = data;
    }

    @Override
    protected void convert(BaseViewHolder helper, FriendsInfo item) {

        //首字母
        TextView tvLetter = helper.getView(R.id.tv_letter);
        tvLetter.setText(item.getLetters());
        if (helper.getLayoutPosition() == 0) {
            tvLetter.setVisibility(View.VISIBLE);
        } else {
            if (item.getLetters().equals(data.get(helper.getLayoutPosition() - 1).getLetters())) {
                tvLetter.setVisibility(View.GONE);
            } else {
                tvLetter.setVisibility(View.VISIBLE);
            }
        }

        //头像
        RoundedImageView ivHead = helper.getView(R.id.iv_head);
        ImageUtil.displayImage(context, ivHead, item.getHeadUrl());

        //姓名
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.getName());
        //电话号码,对中间五位进行*号替换然后显示
        TextView tvPhone = helper.getView(R.id.tv_phone);
        String phone = item.getUserId();
        if (!TextUtils.isEmpty(phone)) {
            String str = "*****";
            StringBuilder sb = new StringBuilder(phone);
            sb.replace(3, 8, str);
            tvPhone.setText(sb.toString());
        }

        //显示的类型,student或者teacher
        TextView tvType = helper.getView(R.id.tv_type);

        //角色
        if ("0".equals(item.getRole())) {
            //学生
            tvType.setText("student");
            tvType.setBackgroundResource(R.drawable.common_friend_student);
        } else if ("2".equals(item.getRole())) {
            //老师
            tvType.setText("teacher");
            tvType.setBackgroundResource(R.drawable.common_friend_teacher);
        } else if ("3".equals(item.getRole())) {
            //校长
            tvType.setText("principal");
            tvType.setBackgroundResource(R.drawable.common_friend_principal);
        } else if ("1".equals(item.getRole())) {
            //家长
            tvType.setText("parent");
            tvType.setBackgroundResource(R.drawable.base_friend_parent);
        }

        //点击事件
        ivHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //头像的点击事件,跳转到个人资料界面
                // TODO: 2018/5/15 0015  
            }
        });

        //整个条目
        LinearLayout llOut = helper.getView(R.id.ll_out);
        llOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //条目的点击事件,跳转到单聊界面,云信的Id由用户ID和用户角色拼接组成
                NimUIKit.startP2PSession(context, item.getUserId() + item.getRole());
            }
        });
    }


    /**
     * 提供给Activity刷新数据
     *
     * @param list
     */
    public void updateList(List<FriendsInfo> list) {
        this.data = list;
        notifyDataSetChanged();
    }

    /**
     * 根据RecyclerView的当前位置获取分类的首字母的char ascii值
     */
    public int getSectionForPosition(int position) {
        return data.get(position).getLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr = data.get(i).getLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

}
